package com.example.campusproject.slice;

import com.example.campusproject.ResourceTable;
import com.example.campusproject.adapter.PageItemAdapter;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Component;
import ohos.agp.components.PageSlider;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;

public class XQPMTAbilitySlice extends AbilitySlice {
    private PageSlider ps;
    private Text[] texts = new Text[4];
    private int[] images={
            ResourceTable.Media_lintongxiaoqu,
            ResourceTable.Media_yantaxiaoqu,
            ResourceTable.Media_qinhanxiaoqu,
            ResourceTable.Media_gaoxinxiaoqu
    };
    private PageItemAdapter adapter;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        this.setUIContent(ResourceTable.Layout_ability_xqpmt);
        texts[0] = (Text) this.findComponentById(ResourceTable.Id_tv_fl);
        texts[1] = (Text) this.findComponentById(ResourceTable.Id_tv_jq);
        texts[2] = (Text) this.findComponentById(ResourceTable.Id_tv_ml);
        texts[3] = (Text) this.findComponentById(ResourceTable.Id_tv_jt);
        ps = (PageSlider) this.findComponentById(ResourceTable.Id_ps);

        for (int i = 0; i < texts.length; i++) {
            texts[i].setTag(i);
            texts[i].setClickedListener(new Component.ClickedListener() {
                @Override
                public void onClick(Component component) {
                    int c= (int) ((Text)component).getTag();
                    for (int j = 0; j < texts.length; j++) {
                        texts[j].setTextColor(Color.BLACK);
                    }
                    texts[c].setTextColor(Color.WHITE);
                    ps.setCurrentPage(c);
                }
            });
        }

        //创建适配器对象，将当前界面对象和封装好的集合发送过去
        adapter=new PageItemAdapter(this,images);
        //将适配器加载至滑动组件上，完成同步组装
        ps.setProvider(adapter);
        ps.addPageChangedListener(new PageSlider.PageChangedListener() {
            @Override
            public void onPageSliding(int i, float v, int i1) {

            }

            @Override
            public void onPageSlideStateChanged(int i) {

            }

            @Override
            public void onPageChosen(int i) {
                for (int j = 0; j < texts.length; j++) {
                    texts[j].setTextColor(Color.BLACK);
                }
                texts[i].setTextColor(Color.WHITE);
            }
        });

    }
}
